package com.sc.test.volley;

import android.graphics.Bitmap;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.sc.test.R;
import com.sc.test.ResultActivity;
import com.sc.test.bean.URLS;
import com.sc.test.utils.Util;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Shen Chao.
 * Created on 2018-06-04-004.
 */

public class VolleyActivity extends AppCompatActivity {
    @BindView(R.id.iv_volley)
    ImageView ivVolley;
    private RequestQueue requestQueue;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_volley);
        ButterKnife.bind(this);
        requestQueue = Volley.newRequestQueue(getApplicationContext());
    }

    @OnClick({R.id.btn_volley_get, R.id.btn_volley_post, R.id.btn_volley_json, R.id.btn_volley_image
            , R.id.btn_volley_imageloader})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_volley_get:
                String get_url = "http://v.juhe.cn/toutiao/index?type=top&key=b8ad08f204f4796f227868cd0b278d50";
                StringRequest get_request = new StringRequest(get_url,
                        new Response.Listener<String>() {
                            @Override
                            public void onResponse(String response) {
                                Util.openNewActivity(VolleyActivity.this, ResultActivity.class, response);
                            }
                        }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                });
                requestQueue.add(get_request);
                break;
            case R.id.btn_volley_post:
                String post_url = "http://v.juhe.cn/toutiao/index";
                StringRequest post_request = new StringRequest(Request.Method.POST, post_url,
                        new Response.Listener<String>() {
                            @Override
                            public void onResponse(String response) {
                                Util.openNewActivity(VolleyActivity.this, ResultActivity.class, response);
                            }
                        }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                }) {
                    @Override
                    protected Map<String, String> getParams() throws AuthFailureError {
                        Map<String, String> map = new HashMap<>();
                        map.put("type", "top");
                        map.put("key", "b8ad08f204f4796f227868cd0b278d50");
                        return map;
                    }
                };
                requestQueue.add(post_request);
                break;
            case R.id.btn_volley_json:
                String json_url = "http://v.juhe.cn/toutiao/index";
                String requestBody = "type=top&key=b8ad08f204f4796f227868cd0b278d50";
                JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, json_url, requestBody,
                        new Response.Listener<JSONObject>() {
                            @Override
                            public void onResponse(JSONObject response) {
//                                Gson gson = new Gson();
//                                News news = gson.fromJson(response.toString(), News.class);
//                                List list = news.getResult().getData();
//                                StringBuffer sb = new StringBuffer();
//                                for (int i = 0; i < list.size(); i++) {
//                                    News.ResultBean.DataBean dataBean = (News.ResultBean.DataBean) list.get(i);
//                                    sb.append(dataBean.getTitle());
//                                }
                                Util.openNewActivity(VolleyActivity.this, ResultActivity.class, response.toString());
                            }
                        }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                });
                requestQueue.add(jsonObjectRequest);
                break;
            case R.id.btn_volley_image:
                ImageRequest imageRequest = new ImageRequest(URLS.IMAGE_URL,
                        new Response.Listener<Bitmap>() {
                            @Override
                            public void onResponse(Bitmap response) {
                                ivVolley.setImageBitmap(response);
                            }
                        }, 0, 0,
                        ImageView.ScaleType.CENTER,
                        Bitmap.Config.ARGB_8888,
                        new Response.ErrorListener() {
                            @Override
                            public void onErrorResponse(VolleyError error) {

                            }
                        }
                );
                requestQueue.add(imageRequest);
                break;
            case R.id.btn_volley_imageloader:
                ImageLoader imageLoader = new ImageLoader(requestQueue, new BitMapCache());
                ImageLoader.ImageListener imageListener = ImageLoader.getImageListener(
                        ivVolley, R.drawable.lodding, R.drawable.error);
                imageLoader.get(URLS.IMAGE_URL, imageListener);
                break;
        }
    }
}
